/*  Copyright (C) 2013   Divya Muthyala & Meenal Nitin Honap.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package com.herps;

import java.util.ArrayList;
import com.herps.data.ListItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class SpeciesAdapter extends ArrayAdapter {

	private Context context;
    private ArrayList<HerpDetails> items;
    private LayoutInflater vi;
    
    public SpeciesAdapter(Context context,ArrayList<HerpDetails> items) {
        super(context,0, items);
        this.context = context;
        this.items = items;
      //get the LayoutInflater for inflating the customomView
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	
    	View v = convertView;
    	 
        final ListItem i = (ListItem) items.get(position);
        if (i != null) {

                HerpDetails orgDetails = (HerpDetails)i;
                //inflate the custom layout
                v = vi.inflate(R.layout.speciesdetails, null);
                
                //set the data to be displayed
                final TextView title = (TextView)v.findViewById(R.id.textView01);
                if (title != null)
                    title.setText(orgDetails.getGenus()+ " " +orgDetails.getEpithet());
                final ImageView imageView = (ImageView)v.findViewById(R.id.imageView01);
                if(imageView != null){
                	String imageName = orgDetails.getGenus() + "_" + orgDetails.getEpithet();
                	imageName = imageName.toLowerCase();
                	imageView.setImageResource(context.getResources().getIdentifier(imageName,"drawable",context.getPackageName()));
                }

        }
      //return the view to be displayed
        return v;
    	
    	
    	
    }
}
